package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.service.OrderSettingService;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;

import com.itheima.utils.POIUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    private static final Logger log = LoggerFactory.getLogger(OrderSettingController.class);
    @Autowired
    private OrderSettingService orderSettingService;

    @RequestMapping("upload")
    public Result upload(MultipartFile excelFile) {
        //POIUTils读取Exceln
        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);

            if (!CollectionUtils.isEmpty(strings)) {
                List<OrderSetting> osList = new ArrayList<>();
                SimpleDateFormat format = new SimpleDateFormat(POIUtils.DATE_FORMAT);
                for (String[] string : strings) {
                    //日期
                    String orderDateStr = string[0];
                    //可预约数
                    Integer number = Integer.valueOf(string[1]);
                    OrderSetting orderSetting = new OrderSetting();
                    //添加数据
                    orderSetting.setNumber(number);
                    orderSetting.setOrderDate(format.parse(orderDateStr));
                    osList.add(orderSetting);
                }
                //调用业务批量导入
                orderSettingService.doImport(osList);
                return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
            }
        } catch (IOException | ParseException e) {
            log.error("批量导入预约设置失败", e);
        }
        return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//参数格式为：2019-03
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            //获取预约设置数据成功
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            //获取预约设置数据失败
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(true, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
